package com.zjy.org.service.impl;

import com.zjy.basic.service.BaseService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.basic.util.PageList;
import com.zjy.org.domain.Department;
import com.zjy.org.domain.Employee;
import com.zjy.org.mapper.DepartmentMapper;
import com.zjy.org.query.DepartmentQuery;
import com.zjy.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.findAll();

        HashMap<Long, Department> map = new HashMap<>();
        for (Department department:departments){
            map.put(department.getId(),department);
        }
        ArrayList<Department> deptTree = new ArrayList<>();
        for(Department department:departments ){
            if (department.getParent_id()==null){//判断为顶级部门
                deptTree.add(department);
            }else {//否则就为次级部门
                Long parent_id = department.getParent_id();
                Department parentDepartment = map.get(parent_id);
                if(parentDepartment!=null){
                    parentDepartment.getChildren().add(department);
                }
            }
        }
        return deptTree;
    }
}
